package com.xj.dao;

import com.xj.model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserDAO {

  String table_name = " user ";
  String insert_field = " name, email, password ";
  String select_field = " id, " + insert_field;

  /**
   * 插入用户
   * @param user
   * @return
   */
  @Insert({"insert into", table_name, "(", insert_field, ") values (#{name},#{email},#{password})"})
  int addUser(User user);

  /**
   * 根据用户id查询用户详情。默认同名映射#{}
   * @param id
   * @return
   */
  @Select({"select", select_field, "from", table_name, "where id=#{id}"})
  User selectById(int id);

  /**
   * 根据用户名字查询用户详情。默认同名映射#{}
   * @param name
   * @return
   */
  @Select({"select", select_field, "from", table_name, "where name=#{name}"})
  User selectByName(String name);

  /**
   * 根据用户邮件查询用户详情。默认同名映射#{}
   * @param email
   * @return
   */
  @Select({"select", select_field, "from", table_name, "where email=#{email}"})
  User selectByEmail(String email);

  /**
   * 传入修改了密码的用户，修改数据库中的密码
   * @param user
   */
  @Update({"update", table_name, "set password=#{password} where id=#{id}"})
  void updatePassword(User user);
}
